package com.base.thread.demo_29;


public class Demo {

	private volatile int signal;
	
	private int get() {
		return signal;
	}
	
	private void set(int signal) {
		this.signal = signal;
	}
	
	public static void main(String[] args) {
		
		Demo demo = new Demo();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName()+" 开始执行.");
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+" 执行完了.");
				demo.set(1);
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName()+" 开始执行.");
				// 等待signal为1开始执行，否则不能执行
				while (demo.get() != 1) {
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				// 当信号为1 的时候，执行代码
				System.out.println(Thread.currentThread().getName()+" 执行完了.");
			}
		}).start();
		
	}
	
}
